package cn.doitedu.day07

import org.apache.spark.SparkConf
import org.apache.spark.sql.{DataFrame, Dataset, SparkSession}

object T01_SQLWordCount {

  def main(args: Array[String]): Unit = {

    //是对SparkContext的增强，里面包装这SparkContext
    val session: SparkSession = SparkSession.builder().appName("SQLWordCount")
      .master("local[4]")
      .getOrCreate()

    import session.implicits._

    val lines: Dataset[String] = session.read.textFile("data/words.txt")

    val words: Dataset[String] = lines.flatMap(_.split(" "))

    words.createTempView("t_wc")

    val df2: DataFrame = session.sql("select value, count(*) counts from t_wc group by value order by counts desc")

    df2.show()
  }

}
